package org.yonggan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.yonggan.entity.User;

@RestController
public class MovieController {

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private LoadBalancerClient loadBalancerClient;

	@GetMapping("/movie/{id}")
	public User findById(@PathVariable Long id) {
		// vip
		return this.restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
	}

	@GetMapping("/test")
	private String test() {

		ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-provider-user");
		System.err.println(" 1 --->"+serviceInstance.getHost() + " : " + serviceInstance.getPort() + " : " + serviceInstance.getServiceId());
		
		ServiceInstance serviceInstance1 = this.loadBalancerClient.choose("microservice-provider-user01");
		System.err.println(" 2 --->" + serviceInstance1.getHost() + " : " + serviceInstance1.getPort() + " : " + serviceInstance1.getServiceId());
		return "1";
	}
}
